﻿using Leeder.Logger;
using LeederGlobal;
using NewAndong.Business;
using NewAndong.Comm;
using NewAndong.Handle;
using NewAndong.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace NewAndong.Forms
{
    public partial class FrmMaterial :  UIPage
    {
        public LogOperate LogObj => Log.GetLogger(this.GetType());
        bool isLoad = true;
        AutoSizeFormClass asc = new AutoSizeFormClass();
        MaterialDirectionBLL bllMaterialDirection = new MaterialDirectionBLL();
        NewAndongHandler NewAndongHandler;
        string _TypeName;

        Dictionary<int, UILabel> DicLabelNames = new Dictionary<int, UILabel>();
        Dictionary<int, UILabel> DicLabelLeftCounts = new Dictionary<int, UILabel>();
        Dictionary<int, UILabel> DicLabelProjectCounts = new Dictionary<int, UILabel>();
        Dictionary<int, UITextBox> DicTextSetCounts = new Dictionary<int, UITextBox>();
        Dictionary<int, UIButton> DicBtnUpdates = new Dictionary<int, UIButton>();

        /// <summary>
        /// 顺引顺建循环数量修改事件
        /// </summary>
        public static event Action ProjectCountChangeEvent;

        public FrmMaterial(string typeName)
        {
            _TypeName = typeName;
            InitializeComponent();
        }

        private void FrmMaterialDirection_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;

            asc.controllInitializeSize(this);
            NewAndongHandler = NewAndongHandler.CreateInstance();
            NewAndongHandler.LipChangeEvent += UiUpdate;
            FrmMaterial2.ProjectChangeEvent += UiUpdate;
            Init_Control();
            UiUpdate();
        }

        private void FrmMaterialDirection_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
        }

        private void Init_Control()
        {
            DicLabelNames.Add(1,  labelName1);
            DicLabelNames.Add(2,  labelName2);
            DicLabelNames.Add(3,  labelName3);
            DicLabelNames.Add(4,  labelName4);
            DicLabelNames.Add(5,  labelName5);
            DicLabelNames.Add(6,  labelName6);
            DicLabelNames.Add(7,  labelName7);
            DicLabelNames.Add(8,  labelName8);
            DicLabelNames.Add(9,  labelName9);
            DicLabelNames.Add(10, labelName10);
            DicLabelNames.Add(11, labelName11);
            DicLabelNames.Add(12, labelName12);
            DicLabelNames.Add(13, labelName13);
            DicLabelNames.Add(14, labelName14);
            DicLabelNames.Add(15, labelName15);
            DicLabelNames.Add(16, labelName16);
            DicLabelNames.Add(17, labelName17);
            DicLabelNames.Add(18, labelName18);
            DicLabelNames.Add(19, labelName19);
            DicLabelNames.Add(20, labelName20);
            DicLabelNames.Add(21, labelName21);
            DicLabelNames.Add(22, labelName22);
            DicLabelNames.Add(23, labelName23);
            DicLabelNames.Add(24, labelName24);
            DicLabelNames.Add(25, labelName25);
            DicLabelNames.Add(26, labelName26);
            DicLabelNames.Add(27, labelName27);
            DicLabelNames.Add(28, labelName28);

            DicLabelLeftCounts.Add(1,  labelLeftCount1);
            DicLabelLeftCounts.Add(2,  labelLeftCount2);
            DicLabelLeftCounts.Add(3,  labelLeftCount3);
            DicLabelLeftCounts.Add(4,  labelLeftCount4);
            DicLabelLeftCounts.Add(5,  labelLeftCount5);
            DicLabelLeftCounts.Add(6,  labelLeftCount6);
            DicLabelLeftCounts.Add(7,  labelLeftCount7);
            DicLabelLeftCounts.Add(8,  labelLeftCount8);
            DicLabelLeftCounts.Add(9,  labelLeftCount9);
            DicLabelLeftCounts.Add(10, labelLeftCount10);
            DicLabelLeftCounts.Add(11, labelLeftCount11);
            DicLabelLeftCounts.Add(12, labelLeftCount12);
            DicLabelLeftCounts.Add(13, labelLeftCount13);
            DicLabelLeftCounts.Add(14, labelLeftCount14);
            DicLabelLeftCounts.Add(15, labelLeftCount15);
            DicLabelLeftCounts.Add(16, labelLeftCount16);
            DicLabelLeftCounts.Add(17, labelLeftCount17);
            DicLabelLeftCounts.Add(18, labelLeftCount18);
            DicLabelLeftCounts.Add(19, labelLeftCount19);
            DicLabelLeftCounts.Add(20, labelLeftCount20);
            DicLabelLeftCounts.Add(21, labelLeftCount21);
            DicLabelLeftCounts.Add(22, labelLeftCount22);
            DicLabelLeftCounts.Add(23, labelLeftCount23);
            DicLabelLeftCounts.Add(24, labelLeftCount24);
            DicLabelLeftCounts.Add(25, labelLeftCount25);
            DicLabelLeftCounts.Add(26, labelLeftCount26);
            DicLabelLeftCounts.Add(27, labelLeftCount27);
            DicLabelLeftCounts.Add(28, labelLeftCount28);

            DicLabelProjectCounts.Add(1,  labelProjectCount1);
            DicLabelProjectCounts.Add(2,  labelProjectCount2);
            DicLabelProjectCounts.Add(3,  labelProjectCount3);
            DicLabelProjectCounts.Add(4,  labelProjectCount4);
            DicLabelProjectCounts.Add(5,  labelProjectCount5);
            DicLabelProjectCounts.Add(6,  labelProjectCount6);
            DicLabelProjectCounts.Add(7,  labelProjectCount7);
            DicLabelProjectCounts.Add(8,  labelProjectCount8);
            DicLabelProjectCounts.Add(9,  labelProjectCount9);
            DicLabelProjectCounts.Add(10, labelProjectCount10);
            DicLabelProjectCounts.Add(11, labelProjectCount11);
            DicLabelProjectCounts.Add(12, labelProjectCount12);
            DicLabelProjectCounts.Add(13, labelProjectCount13);
            DicLabelProjectCounts.Add(14, labelProjectCount14);
            DicLabelProjectCounts.Add(15, labelProjectCount15);
            DicLabelProjectCounts.Add(16, labelProjectCount16);
            DicLabelProjectCounts.Add(17, labelProjectCount17);
            DicLabelProjectCounts.Add(18, labelProjectCount18);
            DicLabelProjectCounts.Add(19, labelProjectCount19);
            DicLabelProjectCounts.Add(20, labelProjectCount20);
            DicLabelProjectCounts.Add(21, labelProjectCount21);
            DicLabelProjectCounts.Add(22, labelProjectCount22);
            DicLabelProjectCounts.Add(23, labelProjectCount23);
            DicLabelProjectCounts.Add(24, labelProjectCount24);
            DicLabelProjectCounts.Add(25, labelProjectCount25);
            DicLabelProjectCounts.Add(26, labelProjectCount26);
            DicLabelProjectCounts.Add(27, labelProjectCount27);
            DicLabelProjectCounts.Add(28, labelProjectCount28);

            DicBtnUpdates.Add(1,  btnUpdate1);
            DicBtnUpdates.Add(2,  btnUpdate2);
            DicBtnUpdates.Add(3,  btnUpdate3);
            DicBtnUpdates.Add(4,  btnUpdate4);
            DicBtnUpdates.Add(5,  btnUpdate5);
            DicBtnUpdates.Add(6,  btnUpdate6);
            DicBtnUpdates.Add(7,  btnUpdate7);
            DicBtnUpdates.Add(8,  btnUpdate8);
            DicBtnUpdates.Add(9,  btnUpdate9);
            DicBtnUpdates.Add(10, btnUpdate10);
            DicBtnUpdates.Add(11, btnUpdate11);
            DicBtnUpdates.Add(12, btnUpdate12);
            DicBtnUpdates.Add(13, btnUpdate13);
            DicBtnUpdates.Add(14, btnUpdate14);
            DicBtnUpdates.Add(15, btnUpdate15);
            DicBtnUpdates.Add(16, btnUpdate16);
            DicBtnUpdates.Add(17, btnUpdate17);
            DicBtnUpdates.Add(18, btnUpdate18);
            DicBtnUpdates.Add(19, btnUpdate19);
            DicBtnUpdates.Add(20, btnUpdate20);
            DicBtnUpdates.Add(21, btnUpdate21);
            DicBtnUpdates.Add(22, btnUpdate22);
            DicBtnUpdates.Add(23, btnUpdate23);
            DicBtnUpdates.Add(24, btnUpdate24);
            DicBtnUpdates.Add(25, btnUpdate25);
            DicBtnUpdates.Add(26, btnUpdate26);
            DicBtnUpdates.Add(27, btnUpdate27);
            DicBtnUpdates.Add(28, btnUpdate28);    
            
            uiLabel100.ForeColor = Color.White;
            uiLabel101.ForeColor = Color.White;
            uiLabel102.ForeColor = Color.White;
            uiLabel103.ForeColor = Color.White;
            uiLabel104.ForeColor = Color.White;
            uiLabel105.ForeColor = Color.White;
            uiLabel106.ForeColor = Color.White;
            uiLabel107.ForeColor = Color.White;
            uiLabel108.ForeColor = Color.White;
            uiLabel109.ForeColor = Color.White;
            uiLabel110.ForeColor = Color.White;
            uiLabel111.ForeColor = Color.White;
            uiLabel112.ForeColor = Color.White;
            uiLabel113.ForeColor = Color.White;
            uiLabel114.ForeColor = Color.White;
            uiLabel115.ForeColor = Color.White;

            foreach (var item in DicTextSetCounts)
            {
                item.Value.RectColor = Color.Black;
            }

            foreach (var item in DicBtnUpdates)
            {
                item.Value.RectColor = Color.Black;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
        }

        private void UiUpdate()
        {
            this.Invoke(new MethodInvoker(() =>
            {
                if (string.IsNullOrEmpty(_TypeName))
                    return;

                //先全部清空
                for (int i = 1; i <= DicLabelNames.Count(); i++)
                {
                    DicLabelNames[i].Text = "";
                    DicLabelLeftCounts[i].Text ="";
                    DicLabelProjectCounts[i].Text = "";
                }

                var list = GolbalVariable.MaterialDirectionBag.Where(x => x.TypeName == _TypeName).OrderBy(x => x.ID).ToList();
                for(int i = 1;i<=list.Count();i++)
                {
                    DicLabelNames[i].Text = list[i - 1].MaterialName;
                    DicLabelLeftCounts[i].Text = list[i - 1].LeftCount.ToString();
                    DicLabelProjectCounts[i].Text = list[i - 1].ProjectCount.ToString();
                }
            }));
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            var btn = (UIButton)sender;
            var index = int.Parse(btn.Name.Replace("btnUpdate", ""));
            var list = GolbalVariable.MaterialDirectionBag.Where(x => x.TypeName == _TypeName).OrderBy(x => x.ID).ToList();
            if(index > list.Count)
                return;
            Edit(list[index - 1]);
        }

        /// <summary>
        /// 修改部品剩余数量输入框
        /// </summary>
        /// <param name="model">部品模型</param>
        private void Edit(MaterialDirectionModel model)
        {
            UIEditOption option = new UIEditOption();
            option.AutoLabelWidth = true;
            option.Text = "循环/剩余台数修改(" + model.MaterialName + ")";
            option.AddInteger("LeftCount", "剩余台数", model.LeftCount, true);
            option.AddInteger("ProjectCount", "循环台数", model.ProjectCount, true);

            UIEditForm frm = new UIEditForm(option);
            frm.ShowDialog();

            if (frm.IsOK)
            {
                model.ProjectCount = int.Parse(frm["ProjectCount"].ToString());
                model.LeftCount = int.Parse(frm["LeftCount"].ToString());
                var sql = string.Format("update LA_MaterialDirection set ProjectCount={0},LeftCount={1} where ID={2}",model.ProjectCount,model.LeftCount,model.ID);
                var result = bllMaterialDirection.UpdateNum(sql);
                if (result>0)
                {
                    UIMessageTip.ShowOk("修改成功");
                    LogObj.Info("修改循环/剩余台数成功(" + model.MaterialName + ");作业者:" + ApplicationCache.CurrentUser.LoginName);
                    ProjectCountChangeEvent?.Invoke();;
                }
                else
                {
                    UIMessageBox.ShowError("修改失败！");
                    LogObj.Error("修改循环/剩余台数失败(" + model.MaterialName + ";" + ");作业者:" + ApplicationCache.CurrentUser.LoginName);
                }
                UiUpdate();
            }
            frm.Dispose();
        }
    }
}
